﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fly.Common.Linq;
using RN.ERP.DTO;


namespace RN.ERP.BLL.QD
{
    public partial class EnterpriseBLL
    {
        public override IList<DTO.CompanyInfo> GetCompanyInfo(
            IList<DTO.QueryCompanyInfoCondition> queryCondition,
            int pageSize,
            int pageNum,
            string areaCondition)
        {
            var dbCompany = dbContext.Corp_CompanyInfos;
            var dbSysUsers = dbContext.T_Sys_Users;
            var query = from company in dbCompany
                        join user in dbSysUsers
                        on company.CorpUserGuid equals user.UserGuid
                        select new CompanyInfo()
                        {
                            Address = company.Address,
                            AreaCode = company.AreaCode,
                            AreaCode2 = company.AreaCode2,
                            bCanBid = company.bCanBid,
                            bISO14000 = company.bISO14000,
                            bISO9000 = company.bISO9000,
                            bISO9001 = company.bISO9001,
                            bOHSMS18000 = company.bOHSMS18000,
                            BussinessArea = company.BussinessArea,
                            BussinessNum = company.BussinessNum,
                            classID = company.classID,
                            CorpGuid = company.CorpGuid,
                            CorpName = company.CorpName,
                            RoleId = user.RoleID,
                            SecurityCode = company.SecurityCode,
                            SecurityTitleGetDate = company.SecurityTitleGetDate,
                            CorpUserGuid = company.CorpUserGuid,
                            Email = company.Email,
                            EndTime = company.EndTime,
                            Fax = company.Fax,
                            FlowCode = company.FlowCode,
                            FoundTime = company.FoundTime,
                            ID = company.ID,
                            Introduction = company.Introduction,
                            Linkman = company.Linkman,
                            LinkMan1 = company.LinkMan1,
                            NetAddress = company.NetAddress,
                            PassNum = company.PassNum,
                            QualificationLevel = company.QualificationLevel,
                            QualificationNum = company.QualificationNum,
                            QualityState = company.QualityState,
                            RegisterMoney = company.RegisterMoney,
                            Tel = company.Tel,
                            Tel1 = company.Tel1,
                            Type = company.Type,
                            ZipCode = company.ZipCode,
                            Dogid = user.RoleID == 105 ? user.KeyId : user.DogID
                        };
            query = query.BuildCondition<CompanyInfo>(queryCondition);
            if (pageSize > 0)
            {
                query = query.Skip(pageNum * pageSize).Take(pageSize);
            }
            return query.ToList(); ;
        }

        public override int GetCompanyCount(IList<QueryCompanyInfoCondition> queryCondition, string areaCondition)
        {
            var dbCompany = dbContext.Corp_CompanyInfos;
            var dbSysUsers = dbContext.T_Sys_Users;
            var query = from company in dbCompany
                        join user in dbSysUsers
                        on company.CorpUserGuid equals user.UserGuid
                        select new CompanyInfo()
                        {
                            Address = company.Address,
                            AreaCode = company.AreaCode,
                            AreaCode2 = company.AreaCode2,
                            bCanBid = company.bCanBid,
                            bISO14000 = company.bISO14000,
                            bISO9000 = company.bISO9000,
                            bISO9001 = company.bISO9001,
                            bOHSMS18000 = company.bOHSMS18000,
                            BussinessArea = company.BussinessArea,
                            BussinessNum = company.BussinessNum,
                            classID = company.classID,
                            CorpGuid = company.CorpGuid,
                            CorpName = company.CorpName,
                            RoleId = user.RoleID,
                            SecurityCode = company.SecurityCode,
                            SecurityTitleGetDate = company.SecurityTitleGetDate,
                            CorpUserGuid = company.CorpUserGuid,
                            Email = company.Email,
                            EndTime = company.EndTime,
                            Fax = company.Fax,
                            FlowCode = company.FlowCode,
                            FoundTime = company.FoundTime,
                            ID = company.ID,
                            Introduction = company.Introduction,
                            Linkman = company.Linkman,
                            LinkMan1 = company.LinkMan1,
                            NetAddress = company.NetAddress,
                            PassNum = company.PassNum,
                            QualificationLevel = company.QualificationLevel,
                            QualificationNum = company.QualificationNum,
                            QualityState = company.QualityState,
                            RegisterMoney = company.RegisterMoney,
                            Tel = company.Tel,
                            Tel1 = company.Tel1,
                            Type = company.Type,
                            ZipCode = company.ZipCode,
                            Dogid = user.RoleID == 105 ? user.KeyId : user.DogID
                        };
            query = query.BuildCondition<CompanyInfo>(queryCondition);
            return query.Count();
        }

        public override IList<CorpQuality> GetCorpQualityInfo(
            IList<QueryCorpQualityInfoCondition> queryCondition,
            int pageSize,
            int pageNum,
            string areaCondition)
        {
            var dbCompany = dbContext.Corp_CompanyInfos;
            var dbQuality = dbContext.Corp_CompanyQualities;
            var dbSysUsers = dbContext.T_Sys_Users;
            var query = from quality in dbQuality
                        join company in dbCompany
                        on quality.CorpUserGuid equals company.CorpUserGuid
                        join user in dbSysUsers
                        on quality.CorpUserGuid equals user.UserGuid
                        where quality.IsDel == false
                        select new CorpQuality()
                        {
                            QualityGuid = quality.QualityGuid,
                            CorpUserGuid = quality.CorpUserGuid,
                            ID = quality.ID,
                            FlowCode = company.FlowCode,
                            DetailName = quality.DetailName,
                            KindID = quality.KindID,
                            GradeName = quality.GradeName,
                            GradeCode = quality.GradeCode,
                            GetDate = quality.GetDate,
                            IsMain = quality.IsMain,
                            CreateDate = quality.CreateDate,
                            AlterDate = quality.AlterDate,
                            CreaterGuid = quality.CreaterGuid,
                            AddFlag = quality.AddFlag,
                            AlterDateTime = quality.AlterDateTime,
                            AlterForwardID = quality.AlterForwardID,
                            CheckComment = quality.CheckComment,
                            Dogid = user.DogID,
                            zzzEndDate = quality.zzzEndDate,
                            QualityCode = quality.QualityCode,
                            zzzhfjg = quality.zzzhfjg,
                            zzzstatus = quality.zzzstatus,
                            Remark = quality.Remark,
                            ImageFile = quality.ImageFile,
                            CorpName = company.CorpName
                        };
            query = query.BuildCondition<CorpQuality>(queryCondition);
            if (pageSize > 0)
            {
                query = query.Skip(pageNum * pageSize).Take(pageSize);
            }
            return query.OrderByDescending(q => q.IsMain).ToList();
        }

        public override int GetCorpQualityCount(IList<QueryCorpQualityInfoCondition> queryCondition, string areaCondition)
        {
            var dbCompany = dbContext.Corp_CompanyInfos;
            var dbQuality = dbContext.Corp_CompanyQualities;
            var dbSysUsers = dbContext.T_Sys_Users;
            var query = from quality in dbQuality
                        join company in dbCompany
                        on quality.CorpUserGuid equals company.CorpUserGuid
                        join user in dbSysUsers
                        on quality.CorpUserGuid equals user.UserGuid
                        where quality.IsDel == false
                        select new CorpQuality()
                        {
                            QualityGuid = quality.QualityGuid,
                            CorpUserGuid = quality.CorpUserGuid,
                            ID = quality.ID,
                            FlowCode = company.FlowCode,
                            DetailName = quality.DetailName,
                            KindID = quality.KindID,
                            GradeName = quality.GradeName,
                            GradeCode = quality.GradeCode,
                            GetDate = quality.GetDate,
                            IsMain = quality.IsMain,
                            CreateDate = quality.CreateDate,
                            AlterDate = quality.AlterDate,
                            CreaterGuid = quality.CreaterGuid,
                            AddFlag = quality.AddFlag,
                            AlterDateTime = quality.AlterDateTime,
                            AlterForwardID = quality.AlterForwardID,
                            CheckComment = quality.CheckComment,
                            Dogid = user.DogID,
                            zzzEndDate = quality.zzzEndDate,
                            QualityCode = quality.QualityCode,
                            zzzhfjg = quality.zzzhfjg,
                            zzzstatus = quality.zzzstatus,
                            Remark = quality.Remark,
                            ImageFile = quality.ImageFile,
                            CorpName = company.CorpName
                        };
            query = query.BuildCondition<CorpQuality>(queryCondition);
            return query.Count();
        }
    }
}
